<?php

namespace App\Models;

use App\Models\Traits\GoldRecordTrait;
use App\Models\Traits\HasScopeInquire;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * App\Models\GoldRecord
 *
 * @property int $id
 * @property int $user_id
 * @property int $gold_seed_id
 * @property int $gold_round_id
 * @property int $gold_product_id
 * @property int $product_id
 * @property int $product_sku_id
 * @property int $up_gold_record_id resold from gold record
 * @property int $round
 * @property string|null $gold_seed_title
 * @property string|null $product_title
 * @property string|null $product_cover
 * @property string|null $sku_name
 * @property string|null $sku_pic
 * @property string|null $sku_attr_value_index
 * @property array|null $sku_attr_value
 * @property string $record_all_price
 * @property string $resell_all_price
 * @property string $pay_net_price
 * @property string $pay_cash_price
 * @property string $pay_gold_beans
 * @property string $profit_after_resold
 * @property \Illuminate\Support\Carbon|null $paid_at
 * @property string|null $paid_method
 * @property \Illuminate\Support\Carbon|null $to_resell_applied_at
 * @property \Illuminate\Support\Carbon|null $to_split_applied_at
 * @property \Illuminate\Support\Carbon|null $to_take_applied_at
 * @property \Illuminate\Support\Carbon|null $settled_at settle up record reselling or split money
 * @property \Illuminate\Support\Carbon|null $rewarded_at
 * @property \Illuminate\Support\Carbon|null $finished_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\GoldProduct|null $goldProduct
 * @property-read \App\Models\GoldRound|null $goldRound
 * @property-read \App\Models\GoldSeed|null $goldSeed
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\GoldSplit[] $goldSplits
 * @property-read int|null $gold_splits_count
 * @property-read \App\Models\Product|null $product
 * @property-read \App\Models\ProductSku|null $sku
 * @property-read GoldRecord|null $upGoldRecord
 * @property-read \App\Models\User|null $user
 * @method static \Database\Factories\GoldRecordFactory factory(...$parameters)
 * @method static Builder|GoldRecord finished()
 * @method static Builder|GoldRecord inquire($condition = null)
 * @method static Builder|GoldRecord newModelQuery()
 * @method static Builder|GoldRecord newQuery()
 * @method static \Illuminate\Database\Query\Builder|GoldRecord onlyTrashed()
 * @method static Builder|GoldRecord paid()
 * @method static Builder|GoldRecord pending()
 * @method static Builder|GoldRecord query()
 * @method static Builder|GoldRecord running()
 * @method static Builder|GoldRecord unpaid()
 * @method static Builder|GoldRecord whereCreatedAt($value)
 * @method static Builder|GoldRecord whereDeletedAt($value)
 * @method static Builder|GoldRecord whereFinishedAt($value)
 * @method static Builder|GoldRecord whereGoldProductId($value)
 * @method static Builder|GoldRecord whereGoldRoundId($value)
 * @method static Builder|GoldRecord whereGoldSeedId($value)
 * @method static Builder|GoldRecord whereGoldSeedTitle($value)
 * @method static Builder|GoldRecord whereId($value)
 * @method static Builder|GoldRecord wherePaidAt($value)
 * @method static Builder|GoldRecord wherePaidMethod($value)
 * @method static Builder|GoldRecord wherePayCashPrice($value)
 * @method static Builder|GoldRecord wherePayGoldBeans($value)
 * @method static Builder|GoldRecord wherePayNetPrice($value)
 * @method static Builder|GoldRecord whereProductCover($value)
 * @method static Builder|GoldRecord whereProductId($value)
 * @method static Builder|GoldRecord whereProductSkuId($value)
 * @method static Builder|GoldRecord whereProductTitle($value)
 * @method static Builder|GoldRecord whereProfitAfterResold($value)
 * @method static Builder|GoldRecord whereRecordAllPrice($value)
 * @method static Builder|GoldRecord whereResellAllPrice($value)
 * @method static Builder|GoldRecord whereRewardedAt($value)
 * @method static Builder|GoldRecord whereRound($value)
 * @method static Builder|GoldRecord whereSettledAt($value)
 * @method static Builder|GoldRecord whereSkuAttrValue($value)
 * @method static Builder|GoldRecord whereSkuAttrValueIndex($value)
 * @method static Builder|GoldRecord whereSkuName($value)
 * @method static Builder|GoldRecord whereSkuPic($value)
 * @method static Builder|GoldRecord whereToResellAppliedAt($value)
 * @method static Builder|GoldRecord whereToSplitAppliedAt($value)
 * @method static Builder|GoldRecord whereToTakeAppliedAt($value)
 * @method static Builder|GoldRecord whereUpGoldRecordId($value)
 * @method static Builder|GoldRecord whereUpdatedAt($value)
 * @method static Builder|GoldRecord whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|GoldRecord withTrashed()
 * @method static \Illuminate\Database\Query\Builder|GoldRecord withoutTrashed()
 * @mixin \Eloquent
 * @property \Illuminate\Support\Carbon|null $closed_at
 * @method static Builder|GoldRecord whereClosedAt($value)
 */
class GoldRecord extends Model
{
    use HasFactory,
	    SoftDeletes,
	    GoldRecordTrait,
	    HasScopeInquire;

    protected $guarded = ['id'];

	protected $casts = [
		'sku_attr_value' => 'json',
	];

    protected $dates = [
    	'closed_at',
    	'paid_at',
    	'to_resell_applied_at',
	    'to_split_applied_at',
	    'to_take_applied_at',
	    'settled_at',
	    'rewarded_at',
	    'finished_at',
    ];

    static array $inquireConditions = [
    	'unpaid' => '待支付',
	    'pending' => '待处理',
	    'running' => '销售中',
	    'finished' => '已完成',
    ];

    public function scopeUnpaid(Builder $builder): Builder
    {
	    return $builder->whereNull('paid_at');
    }

    public function scopePaid(Builder $builder): Builder
    {
	    return $builder->whereNotNull('paid_at');
    }

    public function scopePending(Builder $builder): Builder
    {
    	return $builder->whereNotNull('paid_at')
            ->whereNull('to_resell_applied_at')
            ->whereNull('to_split_applied_at')
            ->whereNull('to_take_applied_at');
    }

    public function scopeRunning(Builder $builder): Builder
    {
	    return $builder->whereNotNull('paid_at')
		    ->whereNull('finished_at')
		    ->where(function (Builder $query) {
			    $query->whereNotNull('to_resell_applied_at')
				    ->orWhereNotNull('to_split_applied_at');
		    });
    }

    public function scopeFinished(Builder $builder): Builder
    {
	    return $builder->whereNotNull('paid_at')->whereNotNull('finished_at');
    }

    public function user(): BelongsTo
    {
    	return $this->belongsTo(User::class);
    }

    public function goldSeed(): BelongsTo
    {
    	return $this->belongsTo(GoldSeed::class);
    }

    public function goldRound(): BelongsTo
    {
    	return $this->belongsTo(GoldRound::class);
    }

    public function goldProduct(): BelongsTo
    {
    	return $this->belongsTo(GoldProduct::class);
    }

    public function goldSplits(): HasMany
    {
        return $this->hasMany(GoldSplit::class, 'split_record_id', 'id');
    }

    public function product(): BelongsTo
    {
    	return $this->belongsTo(Product::class);
    }

    public function sku(): BelongsTo
    {
    	return $this->belongsTo(ProductSku::class, 'product_sku_id', 'id');
    }

    public function upGoldRecord(): BelongsTo
    {
    	return $this->belongsTo(GoldRecord::class, 'up_gold_record_id', 'id');
    }


     public function goldRecordVoucher()
    {
        return $this->belongsTo(GoldPayRecord::class, 'id','gold_record_id');
    }
}
